package com.cictec.middleware.core.service;

import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.entity.BusTimingPointSupply;
import com.cictec.middleware.core.mapper.BusTimingPointSupplyMapper;

/**
 * 位置、进出站原始信息存储服务
 * 
 * @author ql
 *
 */
@Service
public class BusTimingPointService {

	private static Logger logger = LoggerFactory.getLogger(BusTimingPointService.class);

	@Autowired
	private BusTimingPointSupplyMapper busTimingPointSupplyMapper;
	
	


	@PostConstruct
	public void init() {
		int threads = PropertiesUtils.getIntValue("system.parameter.position.threads", 2);
		logger.debug("【 BusTimingPointService 】创建 时序对象存储线程池, 线程池大小{}", threads);
	}
	

	/**
	 *【数据库】查询需要重新计算里程的 车辆信息
	 * @param date	yyyy-MM-dd  不为空时，查询指定时间内的
	 * @param isSupply	true:只查询有补传记录的，false：所有
	 * @return
	 */
	public List<BusTimingPointSupply> selectListBusTimingPointSupply(String date, boolean isSupply){
		
		BusTimingPointSupply supply = new BusTimingPointSupply();
		if(isSupply){
			// 只查询有补传记录的
//			supply.setSupFlag("1");
			supply.setSupRepairFlag("1");
		}
		
		if(!PMSUtils.isEmpty(date)){
			supply.setSupDate(date);
		}
		
		List<BusTimingPointSupply> list = busTimingPointSupplyMapper.select(supply);
		for (BusTimingPointSupply busTimingPointSupply : list) {
			busTimingPointSupply.setDataFrom("db");
		}
		return list;
	}
	
	
	/**
	 * 【数据库】新增，修改数据存储标识
	 * @param devCode
	 * @param date		yyyy-MM-dd
	 * @param supFlag	时序对象补传标识（0：未补传数据，1：有补传数据）
	 * @param supRepairFlag	里程是否以修复（0：默认没有补传或已修复，1:未修复）
	 */
	public void saveTimingPointSupply(String devCode, String date, String supFlag, String supRepairFlag){
		
		BusTimingPointSupply supply = new BusTimingPointSupply();
		supply.setSupDevCode(devCode);
		supply.setSupDate(date);
		
		List<BusTimingPointSupply> supplyList = busTimingPointSupplyMapper.select(supply);
		if(supplyList.size() == 0){
			// 新增
			supply.setSupFlag(supFlag);	
			supply.setSupRepairFlag(supRepairFlag);
		}else{
			// 修改
			supply = supplyList.get(0);
			supply.setSupFlag(supFlag);	
			supply.setSupRepairFlag(supRepairFlag);
		}
		
		
		if(PMSUtils.isEmpty(supply.getSupUuid())){
			supply.setSupUuid(PMSUtils.getUUID());
			busTimingPointSupplyMapper.insertSelective(supply);
		}else{
			busTimingPointSupplyMapper.updateByPrimaryKeySelective(supply);
		}
	}
	
	
	
}
